package com.seguriapp.gwt.client;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.seguriapp.gwt.client.login.LoginPanel;

public class PrincipalHeader extends Composite implements PrincipalHeaderPresenter.Display {

  private static PrincipalHeaderUiBinder uiBinder = GWT.create(PrincipalHeaderUiBinder.class);

  interface PrincipalHeaderUiBinder extends UiBinder<Widget, PrincipalHeader> {
  }

  @UiField
  protected HTMLPanel panelHTML;
  @UiField
  protected InlineLabel welcomeLbl;
  @UiField
  protected InlineLabel lastDateLoggedInlbl;
  @UiField
  protected Anchor loginLink;
  @UiField
  protected Anchor logoffLink;

  private LoginPanel loginPanel;
  private DialogBox dialogBox;

  private PrincipalHeaderPresenter presenter;

  public PrincipalHeader() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  public PrincipalHeader(final String firstName) {
    initWidget(uiBinder.createAndBindUi(this));
  }

  public void setPresenter(final PrincipalHeaderPresenter presenter) {
    this.presenter = presenter;
  }

  @UiHandler("loginLink")
  protected void onLogInLinkClick(final ClickEvent event) {
    presenter.showLoginPopup();
  }

  @UiHandler("logoffLink")
  protected void onLogOffLinkClick(final ClickEvent event) {
    presenter.logOffUser();
  }

  public void showLoginPopup() {
    // Create the dialog box
    final DialogBox dialogBox = createDialogBox();
    dialogBox.setGlassEnabled(true);
    dialogBox.setAnimationEnabled(true);
    dialogBox.center();
    dialogBox.show();
  }

  private DialogBox createDialogBox() {
    // Create a dialog box and set the caption text
    dialogBox = new DialogBox();

    // Add a close button at the bottom of the dialog
    ClickHandler closeHanlder = new ClickHandler() {

      public void onClick(ClickEvent event) {
        dialogBox.hide();
      }
    };
    getLoginPanel();
    loginPanel.setCloseHandler(closeHanlder);
    dialogBox.setWidget(loginPanel);
    // Return the dialog box
    return dialogBox;
  }

  public LoginPanel getLoginPanel() {
    if (loginPanel == null){
      loginPanel = new LoginPanel();
      loginPanel.setSize("350px", "250px");
      loginPanel.addStyleName("loginPopup");
    }
    return loginPanel;
  }

  public void setLoggedInUserInfo(final String username, final Date lastDateLoggedIn) {
    if (dialogBox != null) {
      dialogBox.hide();
    }
    loginLink.setVisible(false);
    logoffLink.setVisible(true);
    welcomeLbl.setText("Bienvenido(a) " + username);
    welcomeLbl.setVisible(true);
    if (lastDateLoggedIn != null) {
      final DateTimeFormat DATE_FORMAT = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");
      lastDateLoggedInlbl.setText("La \u00faltima vez que se logue\u00F3 fue " + 
          DATE_FORMAT.format(lastDateLoggedIn));
      lastDateLoggedInlbl.setVisible(true);
    }
  }

  public void removeUserInformation () {
    loginLink.setVisible(true);
    logoffLink.setVisible(false);
    welcomeLbl.setText("");
    welcomeLbl.setVisible(false);
    lastDateLoggedInlbl.setText("");
    lastDateLoggedInlbl.setVisible(false);
  }

}
